﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Collections;
using BaseHelper;

namespace DeviceSDK.LED.LiMing
{
    public class LiMing_MC100
    {
        private static int ret = -999;

        private static bool IsInit=false;
        private static SortedList<string, int> slIP = new SortedList<string, int>();

        private static LiMing_SDK.TOnMcEvent mcevent;

        private static object locker =new object();
        private static bool Sending = false;

        //获取错误代码
        public static string Get_Error(int ret)
        {

            switch (ret)
            {
                case 0:
                    return "操作成功！";
                case 1:
                    return "操作失败！";
                case 2:
                    return "命令执行超时！";
                case -1:
                    return "本设备不支持此命令！";
                default:
                    return "未知错误！";
            }
        }

        public static Boolean OnMcEvent(int eventno, int boxno,  int para1, int para2 )
        {
            try
            {
                LiMing_SDK.TMcBoxInfo bf = new LiMing_SDK.TMcBoxInfo();
                if (!LiMing_SDK.mcGetBoxInfo(boxno, ref bf))
                    return false;

                switch (eventno)
                {
                    case LiMing_SDK.MCEVENT_CONNECT:
                        {
                            LogHelper.Debug("MC100->OnMcEvent()", string.Format("Connect    ID:{0},CarName:{1},IP:{2},Mcv:{3}", boxno, bf.LedName, bf.IP, bf.mcv));
               
                            if (slIP.ContainsKey(bf.IP))
                                slIP[bf.IP] = boxno;
                            else
                                slIP.Add(bf.IP, boxno);

                            string IP = bf.LedName.Replace("_", ".");
                            if (slIP.ContainsKey(IP))
                                slIP[IP]=boxno;
                            else
                                slIP.Add(IP, boxno);

                        }
                        break;
                    case LiMing_SDK.MCEVENT_DISCONNECT:
                        {
                            LogHelper.Debug("MC100->OnMcEvent()", string.Format("DisConnect    ID:{0},CarName:{1},IP:{2}", boxno, bf.LedName, bf.IP));
                            if (slIP.ContainsKey(bf.IP))
                                slIP.Remove(bf.IP);

                            string IP = bf.LedName.Replace("_", ".");
                            if (slIP.ContainsKey(IP))
                                slIP.Remove(IP);


                        }
                        break;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Err("RT.Helper.Device.LED.LiMing.LiMing_MC100.cs", "OnMcEvent()", ex);
            }


            return true;
        }


        public static bool GetStatus(string IP)
        {
            //LogHelper.Debug("MC100->GetStatus()", "IP:" + IP + ",Is:" + slIP.ContainsKey(IP).ToString());
            return slIP.ContainsKey(IP);
        }
        public static void Init()
        {
            lock ((object)IsInit)
            {
                if (!IsInit)
                {
                    mcevent = OnMcEvent;
                    bool ret = LiMing_SDK.mcInit(8102, mcevent, 0);//8813
                    LogHelper.Debug("MC100->Init()", "8102:" + ret.ToString());

                    ret = LiMing_SDK.mcInit(8813, mcevent, 0);//8813
                    LogHelper.Debug("MC100->Init()", "8813:" + ret.ToString());


                    //ret = LiMing_SDK.mcInit(8104, mcevent, 0);//8813
                    //LogHelper.Debug("MC100->Init()", "8104:" + ret.ToString());

                    IsInit = true;
                }
            }
        }

        public static int Connect(string IP)
        {
            try
            {
                Init();

                if (slIP.ContainsKey(IP))
                    return slIP[IP];

                LiMing_SDK.mcConnectBox(IP, 8100);
                Sending = false;

                int i = 10*5;
                while (i-->0 && !slIP.ContainsKey(IP) )
                {
                    Thread.Sleep(100);
                }

                if (slIP.ContainsKey(IP))
                    return slIP[IP];

                return 0;


            }
            catch(Exception ex)
            {
                LogHelper.Err("RT.Helper.Device.LED.LiMing.LiMing_MC100.cs", "Close()", ex);
            }


            return -1;
        }


        //发送-节目
        public static int Send_Programs(string IP, int Width,int Height,PlaybillProgramInfo[] Programs)
        {
            int id = Connect(IP);
            if (id == 0)
                return 1;


            if (LEDHelper.Release_IsOrderToSend)
            {
                Random rnd = new Random();
                while ((bool)Sending)
                {
                    Thread.Sleep(rnd.Next(500, 1000));
                }
            }

            lock (locker)
            {
                try
                {
                    Sending = true;
                    //bool ret=LiMing_SDK.mcBoxSetAutoConnect(id, "192.168.1.55", 8810);
                    //Console.WriteLine(ret);

                    #region 节目编辑
                    //return ret ? 1 : 0;
                    string cName = IP.Replace(".", "_");
                    string tempFolder = LEDHelper.TempFullPath + cName;

                    //指定节目名以及窗口大小

                    //LiMing_SDK.mcCloseMcv(id);
                    int mcv = LiMing_SDK.mcCreateMcv(LEDHelper.TempFullPath, cName, Width, Height, true, true);
                    if (mcv == 0)
                        return 1;


                    LiMing_SDK.mcSetCurMcv(mcv);
                    //添加节目内容
                    //添加一个节目页


                    foreach (PlaybillProgramInfo pinfo in Programs)
                    {
                        try
                        {
                            int pageno = LiMing_SDK.mcAddPage("", LiMing_SDK.TMcPlayMode.pm_Normal, 1, 0);// pinfo.PlayTime

                            switch (pinfo.Type)
                            {
                                case "picture":
                                    LiMing_SDK.TMcImage img = new LiMing_SDK.TMcImage();
                                    LiMing_SDK.mcGetDefaultImg(ref img);
                                    img.left = pinfo.X;
                                    img.top = pinfo.Y;
                                    img.width = pinfo.Width;
                                    img.height = pinfo.Height;
                                    img.playtime = pinfo.PlayTime;
                                    img.animode = pinfo.IsEffectRand == 0 ? LiMing_SDK.TMcAniMode.ani_Null : LiMing_SDK.TMcAniMode.ani_Rand;
                                    int imgid = LiMing_SDK.mcAddImageBox(pageno, ref img);
                                    LiMing_SDK.mcAddImageFile(imgid, pinfo.FilePath);

                                    break;

                                case "video":
                                    LiMing_SDK.TMcVideo video = new LiMing_SDK.TMcVideo();
                                    LiMing_SDK.mcGetDefaultVideo(ref video);
                                    video.left = pinfo.X;
                                    video.top = pinfo.Y;
                                    video.width = pinfo.Width;
                                    video.height = pinfo.Height;
                                    int videoid = LiMing_SDK.mcAddVideoBox(pageno, ref video);
                                    LiMing_SDK.mcAddVideoFile(videoid, pinfo.FilePath, 100);
                                    break;

                                case "rtf":
                                    LiMing_SDK.TMcNotice notice = new LiMing_SDK.TMcNotice();
                                    LiMing_SDK.mcGetDefaultNotice(ref notice);
                                    notice.left = pinfo.X;
                                    notice.top = pinfo.Y;
                                    notice.width = pinfo.Width;
                                    notice.height = pinfo.Height;
                                    int noticeid = LiMing_SDK.mcAddNoticeBox(pageno, ref notice);
                                    LiMing_SDK.mcAddNoticeRTF(noticeid, pinfo.FilePath, pinfo.IsEffectRand == 0 ? LiMing_SDK.TMcAniMode.ani_Null : LiMing_SDK.TMcAniMode.ani_Rand, 0, 255, 0xFFFFFF, 2, 10);
                                    break;

                                case "text":

                                    int fontcolor = 0;
                                    //FF0000 蓝色
                                    //0x00ff00 绿色
                                    //0x0000ff 红色
                                    //0x00ffff黄
                                    switch (pinfo.FontColor)
                                    {
                                        case "#FF0000":
                                            fontcolor = 0x0000ff;
                                            break;
                                        case "#00FF00":
                                            fontcolor = 0x00ff00;
                                            break;
                                        case "#FFFF00":
                                            fontcolor = 0x00ffff;
                                            break;
                                        default:
                                            fontcolor = 0xffffff;
                                            break;
                                    }

                                    LiMing_SDK.TMcText text = new LiMing_SDK.TMcText();
                                    LiMing_SDK.mcGetDefaultText(ref text);
                                    text.left = pinfo.X;
                                    text.top = pinfo.Y;
                                    text.width = pinfo.Width;
                                    text.height = pinfo.Height;
                                    text.backcolor = 0;
                                    text.alpha = 255;
                                    //text.fontname="楷体";
                                    text.fontcolor = fontcolor;
                                    text.fontname = pinfo.FontName;
                                    text.fontsize = pinfo.FontSize;
                                    text.playtime = pinfo.PlayTime;

                                    LiMing_SDK.mcAddSingleText(pageno, pinfo.Text, -1, ref text);

                                    break;
                            }
                        }
                        catch (Exception ex)
                        {
                            LogHelper.Err("RT.Helper.Device.LED.LiMing.LiMing_MC100.cs", "Send_Programs()", ex);
                        }
                    }
                    #endregion

                    //保存
                    if (LiMing_SDK.mcSaveMcv(mcv))
                    {
                        #region 保存发布
                        LiMing_SDK.mcDeleteMcv(id,cName);

                        if (LiMing_SDK.mcStartSendMcv(id, tempFolder, 1))
                        {
                            int jd;
                            int timeout = LEDHelper.Release_Play_TimeOut * 10; //20s
                            do
                            {
                                jd = LiMing_SDK.mcGetUploadProgress(id);
                                Application.DoEvents();
                                Thread.Sleep(100);
                            } while ((jd < 100) && (timeout-- > 0));


                            if (timeout >= 0 && jd >= 100)
                            {
                                LogHelper.Debug("MC100->Send_Programs()", string.Format("ID:{0},Send Completed!", IP));

                                Application.DoEvents();

                                Sending = false;

                                return 0;
                            }
                            else
                            {

                                LogHelper.Debug("MC100->Send_Programs()", string.Format("ID:{0},jd:{1}%   Fail!", IP, jd));
                            }
                        }
                        #endregion
                    }


                }
                catch (Exception ex2)
                {
                    LogHelper.Err("RT.Helper.Device.LED.LiMing.LiMing_MC100.cs", "Send_Programs().ex2", ex2);
                }
                finally
                {
                    Sending = false;
                }
            }
            
            return 1;
            //return WaitReturn();
        }

        public static void Close()
        {

            try
            {
                LiMing_SDK.mcClose();
            }
            catch (Exception ex)
            {
                LogHelper.Err("RT.Helper.Device.LED.LiMing.LiMing_MC100.cs", "Close()", ex);
            }
            finally
            {

            }
        }

        //设置-重启
        public static int Set_Reboot(string IP)
        {
            int id = Connect(IP);
            if (id > 0)
                return 1;

            LiMing_SDK.mcBoxReboot(id);

            return 0;
        }


        //设置-时间
        public static int Set_Time(string IP)
        {
            int id = Connect(IP);
            if (id > 0)
                return 1;

            return LiMing_SDK.mcBoxSetTime(id, DateTime.Now.ToString()) ? 0 : 1;
        }
    }

    
}
